/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe.util;

import java.io.Serializable;
import java.util.Enumeration;

import javax.portlet.PortletSession;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-3下午05:31:04
 */
public class CommonFieldValidator implements Serializable{

	private static final long serialVersionUID = 3268817446944509027L;
	private static final String COMMON_FIELD_ERROR_NOTICE_=CommonFieldValidator.class.getName()+"_COMMON_FIELD_ERROR_NOTICE_";
	
	private static final String EMPTY_FIELD_ERROR_NOTICE="";

	public static void addErrorNotice(String id,String fieldName,String noticeText,PortletSession portletSession){
		portletSession.setAttribute(COMMON_FIELD_ERROR_NOTICE_+id+"_"+fieldName, noticeText);
	}
	
	public static String getNoticeText(String id,String fieldName,PortletSession portletSession){
		Object noticeTextObj = portletSession.getAttribute(COMMON_FIELD_ERROR_NOTICE_+id+"_"+fieldName);
		if((noticeTextObj!=null)&&(noticeTextObj instanceof String)){
			return (String)noticeTextObj;
		}else{
			return EMPTY_FIELD_ERROR_NOTICE;
		}
	}
	
	public static void clearNoticeText(String id,PortletSession portletSession){
		Enumeration attNames = portletSession.getAttributeNames();
		while(attNames.hasMoreElements()){
			Object attNameObj = attNames.nextElement();
			if((attNameObj!=null)&&(attNameObj instanceof String)){
				String attName = (String)attNameObj;
				if(attName.startsWith(COMMON_FIELD_ERROR_NOTICE_+id+"_")){
					portletSession.removeAttribute(attName);
				}
			}
		}
	}

	public static void clearNoticeText(PortletSession portletSession){
		Enumeration attNames = portletSession.getAttributeNames();
		while(attNames.hasMoreElements()){
			Object attNameObj = attNames.nextElement();
			if((attNameObj!=null)&&(attNameObj instanceof String)){
				String attName = (String)attNameObj;
				if(attName.startsWith(COMMON_FIELD_ERROR_NOTICE_)){
					portletSession.removeAttribute(attName);
				}
			}
		}		
	}

	public static final String REQUEST_PARAM_NULL = "缺少";
	public static final String REQUEST_PARAM_EMPTY = "未填写";
}
